package main

import (
	"fmt"
	"reflect"
)

type reflectionUser struct {
	Id int
	Name string
	Age int
}

func (u reflectionUser) hello() {
	fmt.Println("hello world")
}

func main() {
	u := reflectionUser{
		Id:1,
		Name:"zhang",
		Age:12,
	}
	u.hello()
	info(u)
}

func info(b interface{}){
	rTyp := reflect.TypeOf(b)
	fmt.Println("rType=", rTyp)

	//2. 获取到 reflect.Value
	rVal := reflect.ValueOf(b)
	fmt.Printf("rVal=%v rVal type=%T\n", rVal, rVal)

	//3.获取kind
	kind1 := rVal.Kind()
	kind2 := rTyp.Kind()
	fmt.Println("kind = ", kind1, kind2)

	//反射是在运行时反射，在编译的过程中不会判断出属于什么类型
	iV := rVal.Interface()
	//将 interface{} 通过断言转成需要的类型
	usr, ok := iV.(reflectionUser)
	if ok{
		fmt.Println("usrName=", usr.Name)
		usr.hello()

	}

}